# pip install pymongo -i https://pypi.tuna.tsinghua.edu.cn/simple
# pip install tqdm  -i https://pypi.tuna.tsinghua.edu.cn/simple
# pip install pymysql  -i https://pypi.tuna.tsinghua.edu.cn/simple
# 脚本合并
# pip install pymongo tqdm pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple
from pymongo import MongoClient
import time

from tqdm import tqdm
from bson import json_util
# 记录开始时间
start_time = time.time()

# 连接源库和目标库
source_client = MongoClient(
    'mongodb://dcost:Qa9DUsuePeeDMu@mongo4-0.mongo4.public-service:27017,mongo4-1.mongo4.public-service:27017,mongo4-2.mongo4.public-service:27017?authSource=admin')
target_client = MongoClient(
    'mongodb://dcost:Qa9DUsuePeeDMu@mongo4-0.mongo4.public-service:27017,mongo4-1.mongo4.public-service:27017,mongo4-2.mongo4.public-service:27017?authSource=admin')
source_db = source_client['qylib_archive']
target_db = target_client['gcdp_ods_archive_pro']
# 中南私有化
tables = ['auditStage', 'biz_file_category_dic', 'bookletAttachment', 'buildStandardResult', 'documentInfo',
          'operate_log', 'project', 'project_booklet', 'recyclebin', 'secondCostResult', 'secondCostTemplate', 'stage',
          'systemLayout', 'userLayout']
for table_name in tables:
    # 获取总记录数
    total_records = source_db[table_name].count_documents({})
    print(f"总记录数: {total_records} 当前同步的表名为: {table_name}")
    # 使用 tqdm 显示进度条
    with tqdm(total=total_records, desc="数据同步进度", unit="doc") as pbar:
        for doc in source_db[table_name].find():
            try:
                doc = json_util.loads(json_util.dumps(doc))
                target_db[table_name].replace_one({'_id': doc['_id']}, doc, upsert=True)
            except Exception as e:
                print(f"Error: {e}")
                pass
            pbar.update(1)  # 更新进度条
    # 计算耗时
    total_time = time.time() - start_time
    print(f"数据同步总耗时 Total time: {total_time:.2f} seconds")
    # 增量同步（监听变更流）
    # with source_db.users.watch() as stream:
    #     for change in stream:
    #         if change['operationType'] == 'insert':
    #             target_db.users.insert_one(change['fullDocument'])
    #         elif change['operationType'] == 'update':
    #             target_db.users.update_one(
    #                 {'_id': change['documentKey']['_id']},
    #                 {'$set': change['updateDescription']['updatedFields']}
    #             )
